package wsepForum.logging;

import org.apache.log4j.*;

public class ForumLogger {

	private static Logger logger;

	private static ForumLogger INSTANCE = null;

	public static enum LoggerCategory {DOMAIN, PERSISTANCE, GUI, NETWORK};

	private ForumLogger() {

	}

	public static synchronized ForumLogger getInstance() {
		if(INSTANCE == null) {
			INSTANCE = new ForumLogger();
			Logger.getLogger("org.compass").setLevel(Level.OFF);
			logger = Logger.getLogger(ForumLogger.class.getName());

			DailyRollingFileAppender appender = new DailyRollingFileAppender();
			Layout layout = new PatternLayout("%d{HH:mm:ss,SSS} %p %m%n");
			appender.setFile("forum.log");
			appender.setDatePattern("'.'yyyy-MM-dd");
			appender.setLayout(layout);
			appender.activateOptions();

			logger.addAppender(appender);
			logger.addAppender(new ConsoleAppender(layout));
			logger.setLevel(Level.INFO);
		}
		return INSTANCE;
	}


	private void log(String message, Priority priority, LoggerCategory category) {
		logger.log(priority, String.format("%-13s", category) + "\t" + message);
	}


	public void setPriority(Level level) {
		logger.setLevel(level);
	}

	public void debug(String message, LoggerCategory category){
		log(message, Level.DEBUG, category);
	}

	public void info(String message, LoggerCategory category){
		log(message, Level.INFO, category);
	}

	public void warn(String message, LoggerCategory category){
		log(message, Level.WARN, category);
	}

	public void error(String message, LoggerCategory category){
		log(message, Level.ERROR, category);
	}

	public void fatal(String message, LoggerCategory category){
		log(message, Level.FATAL, category);
	}

	public Object clone(){
		return getInstance();
	}

}
